package com.ssm.home.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.ssm.home.feign.SkuFeignService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

@RestController
@Slf4j
public class HomeController {

    @Resource
    private RestTemplate restTemplate;

    private static final String GET_SKU_INFO_URL = "http://APE-CLOUD-SKU/test";

    @Resource
    private SkuFeignService skuFeignService;

    @GetMapping("test")
    public String test() {
        return "hello Home world";
    }

    @GetMapping("getSkuInfo")
    public String getSkuInfo() {
       return restTemplate.getForObject(GET_SKU_INFO_URL, String.class); //url, 返回值类型
    }

    @GetMapping("getSkuInfo2")
    public String getSkuInfo2() {
        return skuFeignService.test();
    }

    @GetMapping("TestTimeOut")
    public String TestTimeOut() {
        return skuFeignService.timeOut();
    }

    @GetMapping("getHystrix")
    public String testHystrix() {
        return skuFeignService.testHystrix();
    }

    @GetMapping("getHystrixTimeOut")
    @HystrixCommand(fallbackMethod = "testHystrixTimeoutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    })
    public String testHystrixTimeOut() {
        return skuFeignService.testHystrixTimeOut();
    }

    public String testHystrixTimeoutHandler()  {
        return "主动降级了！!";
    }
}
